package xyz.suancaiyu.adminservice;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@SpringBootApplication
@EnableTurbine
@EnableHystrixDashboard
@EnableHystrix
@EnableAdminServer
public class AdminServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminServiceApplication.class, args);
    }


    @Configuration
    public static class SecurityConfig extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.formLogin().loginPage("/login.html")
                    .loginProcessingUrl("/login").permitAll();

            http.logout().logoutUrl("/logout");
            http.csrf().disable();

            http.authorizeRequests()
                    .antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
                    .permitAll();

            http.authorizeRequests().antMatchers("/**").authenticated();

            http.httpBasic();

        }


        @Bean
        public BCryptPasswordEncoder passwordEncoder() {
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            System.out.println("************* 我在xyz.suancaiyu.chapter13.config.SecurityConfig类 ****************************");
            String result = encoder.encode("123456");
            System.out.println("原始密码:123456,加密之后的密码:" + result);
            return new BCryptPasswordEncoder();
        }

        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth)throws Exception{
            auth.inMemoryAuthentication()
                    .withUser("admin").password(passwordEncoder().encode("123456")).roles("User");
            // throw new RuntimeException("admin-service: AdminServiceApplication[52]: 需要设置密码!");
        }


    }


}

